package com.bianxianmao.utils;

import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
    /**
     * 判断电话号码是否符合格式.
     *
     * @param inputText the input text
     * @return true, if is phone
     */
    public static boolean isPhone(String inputText) {
        //手机号的正则表达式
        Pattern p = Pattern.compile("^1[3,4,5,7,8]\\d{9}$");
        Matcher m = p.matcher(inputText);
        return m.matches();
    }

    public static boolean isIdentifycod(String inputText) {
        //6位数的正则表达式
        Pattern p1 = Pattern.compile("^[0-9]{6}$");
        Matcher m1 = p1.matcher(inputText);
        return m1.matches();
    }

    public static boolean isPassword(String inputText) {
        //6-20位密码和数字的正则表达式
        Pattern p2 = Pattern.compile("^[0-9A-Za-z]{6,20}$");
        Matcher m2 = p2.matcher(inputText);
        return m2.matches();
    }

    //读取资源文件
    public static String geFileFromAssets(Context context, String fileName) {
        if (context == null || TextUtils.isEmpty(fileName)) {
            return null;
        }

        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}